package com.sqs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sqs.constant.ExceptionConstant;
import com.sqs.context.BaseContext;
import com.sqs.entity.ChatMessage;
import com.sqs.entity.Session;
import com.sqs.exception.SessionNotFoundException;
import com.sqs.mapper.ChatMessageMapper;
import com.sqs.mapper.SessionMapper;
import com.sqs.service.ISessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqs.vo.ChatMessageVO;
import com.sqs.vo.SessionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 会话 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-06-15
 */
@Service
public class SessionServiceImpl extends ServiceImpl<SessionMapper, Session> implements ISessionService {

    @Autowired
    private SessionMapper sessionMapper;
    @Autowired
    private ChatMessageMapper chatMessageMapper;

    /**
     * 查询用户的全部会话
     * @return
     * */
    public List<SessionVO> sessionsSelect() {
        List<Session> sessions = lambdaQuery()
                .eq(Session::getUserId, BaseContext.getCurrentId())
                .orderByDesc(Session::getUpdateTime)
                .list();
        return BeanUtil.copyToList(sessions, SessionVO.class);
    }

    /**
     * 创建新的会话
     * @param name
     * @return
     * */
    public SessionVO sessionInsert(String name) {
        Session session = Session.builder()
                .userId(BaseContext.getCurrentId())
                .name(name)
                .build();
        save(session);
        return BeanUtil.copyProperties(session, SessionVO.class);
    }

    /**
     * 更改会话名称
     * @param id
     * @param name
     * */
    public void sessionUpdate(Long id, String name) {
        //1.判断session是否存在
        if(!lambdaQuery().eq(Session::getId, id).exists()){
            throw new SessionNotFoundException(ExceptionConstant.SESSION_NOT_FOUND);
        }
        //2.更改session名称
        Session session = Session.builder()
                .id(id)
                .name(name)
                .updateTime(LocalDateTime.now())
                .build();
        sessionMapper.updateById(session);
    }

    /**
     * 查询会话的对话记录
     * @param id
     * @return
     * */
    public List<ChatMessageVO> chatMessageSelect(Long id) {
        //1.查询对话记录
        LambdaQueryWrapper<ChatMessage> chatMessageLambdaQueryWrapper = Wrappers
                .lambdaQuery(ChatMessage.class)
                .eq(ChatMessage::getSessionId, id)
                .orderByAsc(ChatMessage::getCreateTime);
        List<ChatMessage> chatMessages = chatMessageMapper.selectList(chatMessageLambdaQueryWrapper);
        return BeanUtil.copyToList(chatMessages, ChatMessageVO.class);
    }


}
